package com.example.studentknowledgesys.service.impl;

import com.example.studentknowledgesys.dao.PointRecordMapper;
import com.example.studentknowledgesys.dao.PointRuleMapper;
import com.example.studentknowledgesys.dao.UserPointMapper;
import com.example.studentknowledgesys.entity.PointRecord;
import com.example.studentknowledgesys.entity.PointRule;
import com.example.studentknowledgesys.entity.UserPoint;
import com.example.studentknowledgesys.service.PointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;


/**
 * @author 光的代言人
 */
@Service("pointService")
public class PointServiceImpl implements PointService {
    @Autowired
    private PointRuleMapper pointRuleMapper;
    @Autowired
    private UserPointMapper userPointMapper;
    @Autowired
    private PointRecordMapper pointRecordMapper;

    /**
     * 创建积分规则
     *
     * @param pointRule 积分规则对象
     * @return 创建的积分规则对象
     */
    @Override
    @Transactional
    public PointRule createPointRule(PointRule pointRule) {
        // 设置默认值
        if (pointRule.getIsActive() == null) {
            pointRule.setIsActive(true);
        }

        pointRuleMapper.insertPointRule(pointRule);
        return pointRule;
    }
    /**
     * 更新积分规则
     *
     * @param pointRule 积分规则对象
     * @return 更新的积分规则对象
     */
    @Override
    @Transactional
    public PointRule updatePointRule(PointRule pointRule) {
        PointRule existing = pointRuleMapper.selectPointRuleById(pointRule.getId());
        if (existing == null) {
            throw new RuntimeException("积分规则不存在");
        }

        pointRuleMapper.updatePointRule(pointRule);
        return pointRuleMapper.selectPointRuleById(pointRule.getId());
    }
    /**
     * 删除积分规则
     *
     * @param id 积分规则ID
     * @return 是否删除成功
     */
    @Override
    @Transactional
    public boolean deletePointRule(Integer id) {
        PointRule rule = pointRuleMapper.selectPointRuleById(id);
        if (rule == null) {
            return false;
        }

        pointRuleMapper.deletePointRule(id);
        return true;
    }
    /**
     * 获取所有积分规则
     *
     * @return 所有积分规则列表
     */
    @Override
    public List<PointRule> getAllPointRules() {
        return pointRuleMapper.selectAllPointRules();
    }
    /**
     * 获取所有激活的积分规则
     *
     * @return 所有激活的积分规则列表
     */
    @Override
    public List<PointRule> getActivePointRules() {
        return pointRuleMapper.selectActivePointRules();
    }
    /**
     * 获取用户积分
     *
     * @param userId 用户ID
     * @return 用户积分对象
     */
    @Override
    public UserPoint getUserPoint(Long userId) {
        return userPointMapper.selectUserPointByUserId(userId);
    }
    /**
     * 获取积分排行榜
     *
     * @param limit 排行榜限制数量
     * @return 排行榜列表
     */
    @Override
    public List<UserPoint> getTopUsers(int limit) {
        return userPointMapper.selectTopUsers(limit);
    }
    /**
     * 获取用户积分记录
     *
     * @param userId 用户ID
     * @return 用户积分记录列表
     */
    @Override
    public List<PointRecord> getUserPointRecords(Long userId) {
        return pointRecordMapper.selectPointRecordsByUserId(userId);
    }
    /**
     * 获取所有积分记录
     *
     * @return 所有积分记录列表
     */
    @Override
    public List<PointRecord> getAllPointRecords() {
        return pointRecordMapper.selectAllPointRecords();
    }
    /**
     * 授予积分
     *
     * @param userId      用户ID
     * @param ruleType    积分规则类型
     * @param description 描述
     */
    @Override
    @Transactional
    public int awardPoints(Long userId, String ruleType, String description) {
        // 获取规则
        PointRule rule = pointRuleMapper.selectActiveRuleByType(ruleType);
        if (rule == null) {
            throw new RuntimeException("未找到有效的积分规则: " + ruleType);
        }

        // 奖励积分
        return awardPoints(userId, ruleType, rule.getPointValue(), description);
    }
    /**
     * 授予积分
     *
     * @param userId      用户ID
     * @param ruleType    积分规则类型
     * @param points      积分数量
     * @param description 描述
     */
    @Override
    @Transactional
    public int awardPoints(Long userId, String ruleType, int points, String description) {
        // 获取用户积分
        UserPoint userPoint = userPointMapper.selectUserPointByUserId(userId);
        if (userPoint == null) {
            // 初始化用户积分
            userPoint = new UserPoint();
            userPoint.setUserId(userId);
            userPoint.setTotalPoints(0);
            userPointMapper.insertUserPoint(userPoint);
            userPoint = userPointMapper.selectUserPointByUserId(userId);
        }

        // 更新用户积分
        int newPoints = userPoint.getTotalPoints() + points;
        userPoint.setTotalPoints(newPoints);
        userPointMapper.updateUserPoint(userPoint);

        // 创建积分记录
        PointRule rule = pointRuleMapper.selectActiveRuleByType(ruleType);
        Integer ruleId = (rule != null) ? rule.getId() : null;

        PointRecord record = new PointRecord();
        record.setUserId(userId);
        record.setRuleId(ruleId);
        record.setPoints(points);
        record.setDescription(description);
        pointRecordMapper.insertPointRecord(record);

        // 返回实际奖励的积分值
        return points;
    }

    @Override
    public PointRule getRuleByType(String ruleType) {
        return pointRuleMapper.selectActiveRuleByType(ruleType);
    }

    @Override
    public List<UserPoint> getAllUserPoints() {
        return userPointMapper.selectAllUserPoints();
    }
}